package com.caigou.config;

import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.cj.jdbc.MysqlXADataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.jta.JtaTransactionManager;

import javax.sql.DataSource;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;

import java.sql.SQLException;

@Slf4j
@Configuration
// 扫描的Dao层
@MapperScan(basePackages = "com.caigou.mapper.exam", sqlSessionTemplateRef = "examSqlSessionTemplate")
public class ExamDatasourceConfig {

    // 配置数据源
    @Bean(name = "examDataSource",destroyMethod = "close",initMethod = "init")
    @DependsOn({"txManager"})
    public AtomikosDataSourceBean examDataSource(ExamConfig examConfig) throws SQLException {
        // 创建MYsql实现XA规范的分布式数据源
        MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
        // 设置连接信息
        mysqlXaDataSource.setUrl(examConfig.getUrl());
        mysqlXaDataSource.setPassword(examConfig.getPassword());
        mysqlXaDataSource.setUser(examConfig.getUsername());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

        // 数据源改为Atomikos，将事务交给Atomikos统一管理
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(mysqlXaDataSource);
        log.info("exam url={}, minPooSize={}, maxPoolSize={}", examConfig.getUrl(), examConfig.getMinPoolSize(), examConfig.getMaxPoolSize());
        xaDataSource.setMinPoolSize(examConfig.getMinPoolSize());
        xaDataSource.setMaxPoolSize(examConfig.getMaxPoolSize());
        xaDataSource.setTestQuery(examConfig.getTestQuery());
        xaDataSource.setUniqueResourceName("examDataSource");
        return xaDataSource;
    }

    @Bean(name = "examSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("examDataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/exam/*.xml"));
        // 通常项目中配置为*，扫描所有的mapper
        // bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper1/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "examSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("examSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
